package top.fengleifeng.question;

import java.util.Stack;

/**
 * @author : fengelfeing
 * @version V1.0
 * @Project: leetcode-test
 * @Package : top.fengleifeng.question
 * @Description:
 * @Date: 2020年11月05日7:18 下午
 */
public class Num1574删除最短的子数组使剩余数组有序 {

    //非递减
    class Solution {

        public int findLengthOfShortestSubarray(int[] arr) {
            int start = 0;
            int end = arr.length - 1;
            while (start < arr.length-1 && arr[start + 1] >= arr[start]) {
                start++;
            }
            while (end >= 1 && arr[end] >= arr[end - 1]) {
                end--;
            }
            if(start>=end) return 0;

            int s = start;
            int e = arr.length;
            int minlength = arr.length;
            while (s >= 0) {
                while (e > 0 && arr[s] <= arr[e - 1] && e > end) {
                    e--;
                }
                minlength = Math.min(minlength, e - s - 1);
                s--;
            }
            minlength=Math.min(minlength,end);
            return minlength;
        }
    }

    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 10, 4, 2, 3, 5};
        new Num1574删除最短的子数组使剩余数组有序().new Solution().findLengthOfShortestSubarray(ints);
    }
}
